package com.zhongying.inteceptor.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.zhongying.inteceptor.entity.RequestLogEntity;
import com.zhongying.sys.dao.SysMenuDao;
import com.zhongying.sys.dao.SysOperateLogDao;
import com.zhongying.sys.entity.SysMenuEntity;

@Component
public class RequestLogUtil {

	private static SysMenuDao sysMenuDao;
	
	
	private static SysOperateLogDao sysOperateLogDao;
	
	@Autowired
	public void setSysMenuDao(SysMenuDao sysMenuDao) {
		RequestLogUtil.sysMenuDao = sysMenuDao;
	}
	
	@Lazy
	@Autowired
	@Qualifier("sysOperateLogDao")
	public void setSysOperateLogDao(SysOperateLogDao sysOperateLogDao) {
		RequestLogUtil.sysOperateLogDao = sysOperateLogDao;
	}
	
	public static void saveLog(RequestLogEntity log) {
		sysOperateLogDao.insert(log);
	}

	public static List<RequestLogEntity> list(String url, String name,Integer s,Integer limit,String start,String end) {
		return sysOperateLogDao.list(url, name, s, limit,start,end);
	}
	
	
	public static Long count(String url, String name,String start,String end) {
		return sysOperateLogDao.count(url,name,start,end);
	}
	
	public static List<SysMenuEntity> queryByUrl(String url) {
		Map<String,Object> params = new HashMap<>();
		params.put("url", url);
		return sysMenuDao.queryList(params);
	}
	
	
	public static List<SysMenuEntity> queryByPerm(String perm) {
		Map<String,Object> params = new HashMap<>();
		params.put("perm", perm);
		return sysMenuDao.queryList(params);
	}
	
	public String listSysOperateLog(String url, String name,Integer s,Integer limit,String start,String end){
		return withLimit(new SQL() {
			{
				SELECT("id,operate_data,operate_url,operate_business,operate_desc,operate_ip,operate_user,operate_time");
				FROM("sys_operate_log");
				if (StringUtils.isNotBlank(url)) {
					WHERE("operate_url like " + assemblyStr("%"+url+"%"));
				}
				
				if (StringUtils.isNoneBlank(name)) {
					WHERE("operate_user = " +assemblyStr(name));
				}
				
				if (StringUtils.isNotBlank(start)) {
					WHERE("operate_time >= " +assemblyStr(start));
				}
				
				if (StringUtils.isNotBlank(end)) {
					WHERE("operate_time <= " +assemblyStr(end));
				}
				
				ORDER_BY("operate_time desc");
			}
		}.toString(),s,limit);
	}
	
	
	public String countSysOperateLog(String url, String name,String start,String end) {
		return new SQL() {{
			SELECT("count(id)");
			FROM("sys_operate_log");
			if (StringUtils.isNotBlank(url)) {
				WHERE("operate_url like " + assemblyStr("%"+url+"%"));
			}
			
			if (StringUtils.isNoneBlank(name)) {
				WHERE("operate_user = " +assemblyStr(name));
			}
			if (StringUtils.isNotBlank(start)) {
				WHERE("operate_time >= " +assemblyStr(start));
			}
			
			if (StringUtils.isNotBlank(end)) {
				WHERE("operate_time <= " +assemblyStr(end));
			}
		}}.toString();
	}
	
	/**
	 * 创建mybatis代理对象
	 * @return
	 */
	@Bean(name="sysOperateLogDao")
	@Autowired
	public MapperFactoryBean<SysOperateLogDao> getSysOperateLogMapper(SqlSessionFactory sqlSessionFactory) {
		MapperFactoryBean<SysOperateLogDao> mapper = new MapperFactoryBean<>();
		mapper.setMapperInterface(SysOperateLogDao.class);
		mapper.setSqlSessionFactory(sqlSessionFactory);
		return mapper;
	}
	
	public static String assemblyStr(Object value) {
		if (value == null) {
			return null;
		}
		if (value instanceof Date) {
			String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) value);
			return "'" + dateStr + "'";
		}
		return "'" + String.valueOf(value) + "'";
	}
	
	public static String withLimit(String sql, Integer start, Integer limit) {
		return sql + " limit " + start + "," + limit;
	}
}
